/**
 * Created by gakuin on 5/16/16.
 */
import {combineReducers} from 'redux-immutable';
import {reducerCreator} from '../utils/creator'
import Immutable from 'immutable'
import * as types from '../constance/actionType.js'

export default combineReducers({
    bannerlist: reducerCreator(Immutable.fromJS({data:[],cates:{cates_1:[],cates_2:[]}}), {
        [types.RECEIVE_BANNER_LIST](state, {data,cates}){
            if(cates==null)
                cates={cates_1:[],cates_2:[]};
            return state.set('data',data).set('cates',cates);
        }
    }),
    storeList: reducerCreator(Immutable.fromJS([]), {
        [types.RECEIVE_STORE_LIST](state, {data}){
            let newState = Immutable.fromJS(data);
            return newState;
        }
    }),
    valueCardList: reducerCreator(Immutable.fromJS({data: [], page: 1,isNeedFetch:true}), {
        [types.RECEIVE_VALUE_CARD_LIST](state, {data,page}){
            var card_count=data.length;
            let newState=state;

            if(page!=1)
                data=state.get('data').concat(data);
            if(card_count<10)
                newState = state.set('data',data).set('page', page).set('isNeedFetch',false);
            else{
                newState = state.set('data',data).set('page', page);
            }

            return newState;
        }
    }),

    cateCardList: reducerCreator(Immutable.fromJS({data: [], page: 1,isNeedFetch:true,cate_id:0}), {
        [types.RECEIVE_CATE_CARD_LIST](state, {data,page,cate_id}){
            var card_count=data.length;//取得的卡券数量
            let newState=state;
            //如果分类ID不一致替换内容
            if(state.get("cate_id")==cate_id&&page!=1) {
                data = state.get('data').concat(data);
            }
            //console.log(card_count);
            if(card_count<10)
                newState = state.set('data',data).set('page', page).set("cate_id",cate_id).set('isNeedFetch',false);
            else{
                //切换是重置needfetch 避免新页面无法加载更多
                newState = state.set('data',data).set('page', page).set("cate_id",cate_id).set('isNeedFetch',true);
            }

            return newState;
        }
        //,
        //[types.CHANGE_CATE](state, {cate_id}){
        //    //console.log("reducer change cate");
        //    let newState = state.set('data',[]).set('page', 1).set("cate_id",cate_id).set('isNeedFetch',true);
        //
        //    return newState;
        //}
    }),
    myCardList: reducerCreator(Immutable.fromJS({}), {
        [types.RECEIVE_MY_CARD_LIST](state, {data}){
            let newState = state.concat(data);
            return newState;
        }
    }),
    valueCardDetail: reducerCreator(Immutable.fromJS({}), {
        [types.RECEIVE_VALUE_CARD_DETAIL](state, {data}){
            let newState = state.concat(data);
            return newState;
        }
    }),
    // data:{},fetchEnd:false
    //getOrderAndPayResult: reducerCreator(Immutable.fromJS({}), {
    //    [types.RECEIVE_GET_ORDER_AND_PAY](state, {data,payend}){
    //        let newState = state.merge(data).merge({payend:payend});
    //        return newState;
    //    }
    //    , [types.PAY_END](state, {data}){
    //        let newState = state.merge({payend:data});
    //        return newState;
    //    },[types.CLEAN_PAY_END](state,{data}){
    //        let newState = state.merge({payend:data});
    //        return newState;
    //    }
    //}),
    //myUnusedCardList: reducerCreator(Immutable.fromJS({payend: false}), {
    myUnusedCardList: reducerCreator(Immutable.fromJS({code:null,data:[]}), {
        [types.RECEIVE_MY_UNUSED_CARD_LIST](state, {data}){
            let newState = state.merge(data);
            return newState;
        }
    }),
    myNotPayList: reducerCreator(Immutable.fromJS({code:null,data:[]}), {
        [types.RECEIVE_MY_NOTPAY_CARD_LIST](state, {data}){
            let newState = state.merge(data);
            return newState;
        },
        [types.RECEIVE_CANCEL_ORDER](state, {data}){ //取消订单 重新加载未支付
            if(data.code==0){
                let newState = state.merge({code:null,data:[]});
                return newState;
            }else{
                return state;
            }
        },
    }),
    myUsedCardList: reducerCreator(Immutable.fromJS({code:null,data:[]}), {
        [types.RECEIVE_MY_USED_CARD_LIST](state, {data}){
            let newState = state.merge(data);
            return newState;
        }
    }),
    myOverdueCardList: reducerCreator(Immutable.fromJS({code:null,data:[]}), {
        [types.RECEIVE_MY_OVERDUE_CARD_LIST](state, {data}){
            let newState = state.merge(data);
            return newState;
        }
    }),
    continuePayResultCode: reducerCreator(Immutable.fromJS({}), {
        [types.RECEIVE_CONTINUE_PAY](state, {data}){
            let newState = state.merge(data);
            return newState;
        }
    }),

    cancelOrderResultCode: reducerCreator(Immutable.fromJS({code: -1}), {
        [types.RECEIVE_CANCEL_ORDER](state, {data}){
            let newState = state.merge(data);
            return newState;
        }
    })


})
